#include <QListWidgetItem>
#include "SettingsView.h"
#include "ui_SettingsView.h"
#include "SettingsViews/Appearance.h"
#include "SettingsViews/Common.h"
#include "SettingsViews/Interpreter.h"

SettingsView::SettingsView(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::SettingsView)
{
    ui->setupUi(this);
    connect(ui->menu, SIGNAL(currentRowChanged(int)), SLOT(menuActivated(int)));
    addView(new Common(this));
    addView(new Interpreter(this));
    addView(new Appearance(this));

    connect(ui->buttonBox, SIGNAL(clicked(QAbstractButton*)), SLOT(doBoxAction(QAbstractButton*)));

    menuActivated(0);
}

SettingsView::~SettingsView()
{
    delete ui;
}

void SettingsView::addView(ISettingsView *view)
{
    _views.append(view);
    view->hide();
    new QListWidgetItem(view->icon(), view->displayName(), ui->menu);
}

void SettingsView::menuActivated(int row)
{
    ui->place->takeWidget();
    ui->place->setWidget(_views[row]);
}

void SettingsView::doBoxAction(QAbstractButton* btn)
{
    switch(ui->buttonBox->buttonRole(btn)){
    case QDialogButtonBox::AcceptRole:
        saveSettings();
        accept();
        break;
    case QDialogButtonBox::ApplyRole:
        saveSettings();
        break;
    default:
        reject();
    }
}

void SettingsView::saveSettings()
{
    foreach(ISettingsView *page, _views){
        page->save();
    }
}
